import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * Created by huangweicheng on 2018/5/21.
 */

public class hbase {

    private static Configuration conf = HBaseConfiguration.create();

    static {
//        conf.set("hbase.bigdata", "192.168.159.143:16010");
        // 设置Zookeeper,直接设置IP地址
        conf.set("hbase.zookeeper.quorum", "192.168.159.143");
//        conf.set("hbase.zookeeper.property.clientPort", "2181");
    }
    // 创建表
    public static void createTable(String tablename, String columnFamily) throws Exception {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        TableName tableNameObj = TableName.valueOf(tablename);
        if (admin.tableExists(tableNameObj)) {
            System.out.println("Tableexists!");
            System.exit(0);
        } else {
            HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
            tableDesc.addFamily(new HColumnDescriptor(columnFamily));
            admin.createTable(tableDesc);
            System.out.println("createtable success!");
        }
        admin.close();
        connection.close();
    }

    // 删除表
    public static void deleteTable(String tableName) {
        try {
            Connection connection =ConnectionFactory.createConnection(conf);
            Admin admin = connection.getAdmin();
            TableName table = TableName.valueOf(tableName);
            admin.disableTable(table);
            admin.deleteTable(table);
            System.out.println("deletetable " + tableName + "ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 插入一行记录
    public static void addRecord(String tableName, String rowKey,String family, String qualifier,String value){
        try {
            Connection connection =ConnectionFactory.createConnection(conf);
            Table table = connection.getTable(TableName.valueOf(tableName));
            Put put = new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));
            put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier), Bytes.toBytes(value));
            table.put(put);
            table.close();
            connection.close();
            System.out.println("insertrecored " + rowKey + " totable " + tableName + "ok.");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //运行
    public static void main(String[] args) throws Exception {
        hbase.createTable("testTb", "info");
        hbase.addRecord("testTb", "001", "info", "name", "zhangsan");
        hbase.addRecord("testTb", "001", "info", "age", "20");
        //HbaseDao.deleteTable("testTb");

    }





}
